package com.ce.cetag.tags.jssupport;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.ce.cetag.util.UiString;





/**
 *
 * <p>
 * 中企动力
 * </p>
 *
 * @author��˧�� 贺帅彪
 *
 * @create date : 2007-7-27
 */
public class JsTag extends TagSupport {

	private static final long serialVersionUID = 1L;

	private String url;

	private String parameters;

	public String getParameters() {
		return parameters;
	}

	public void setParameters(String parameters) {
		this.parameters = parameters;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int doStartTag() {
		return 0;

	}

	public String getFileContent() {
		String realPath = pageContext.getRequest().getRealPath("/");
		String tempUrl = "file:///" + realPath + getUrl();

		try {
			URL realUrl = new URL(tempUrl);
			InputStream is = realUrl.openStream();
			BufferedReader infile = new BufferedReader(
					new InputStreamReader(is,"UTF-8"));
			String inLine;
			StringBuffer fileStr = new StringBuffer();
			while ((inLine = infile.readLine()) != null) {

				fileStr.append(inLine + '\n');
			}
			return fileStr.toString();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public int doEndTag() throws JspException {

		JspWriter out = pageContext.getOut();
		String[] strs = parameters.split(",");
		String inputJs = UiString.simpleConstruct(getFileContent(), strs);
		try {
			out.write(inputJs);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return super.doEndTag();
	}
}
